package com.wenhuitec.android.tang.ui.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.ui.activity.Activity4;

import java.util.ArrayList;
import java.util.List;

public class KeyAdapter extends BaseAdapter {

    private Context mContext;
    private List<Integer> listNumber = new ArrayList<>();
    private KeyOnClick mKeyNum;

    public void setKeyClick(KeyOnClick mKeyNum) {
        this.mKeyNum = mKeyNum;
    }

    public KeyAdapter(Context context) {
        this.mContext = context;
        listNumber.clear();
        listNumber.add(104);
        listNumber.add(105);
        listNumber.add(106);
        listNumber.add(101);
        listNumber.add(102);
        listNumber.add(103);
        for (int i = 1; i <= 9; i++) {
            listNumber.add(i);
        }
        listNumber.add(10);
        listNumber.add(0);
    }

    public KeyAdapter(Context mContext, List<Integer> listNumber) {
        this.mContext = mContext;
        this.listNumber = listNumber;
    }

    @Override
    public int getCount() {
        return listNumber.size();
    }

    @Override
    public Object getItem(int position) {
        return listNumber.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.view_number_gridview_item, null);
            holder = new ViewHolder();
            holder.btnNumber = (TextView) convertView.findViewById(R.id.btNumber);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //-------------设置数据----------------
        holder.btnNumber.setText(listNumber.get(position) + "");
//            if (position == 9) {
//                holder.btnNumber.setText("");
//                holder.btnNumber.setBackgroundColor(mContext.getResources().getColor(R.color.graye3));
//            }
//            if (position == 11) {
//                holder.btnNumber.setText("");
////                holder.btnNumber.setBackgroundResource(listNumber.get(position));
//            }
        switch (listNumber.get(position)) {
            case 101:
                holder.btnNumber.setText("+");
                break;
            case 102:
                holder.btnNumber.setText("-");
                break;
            case 103:
                holder.btnNumber.setText("=");
                break;
            case 104:
                holder.btnNumber.setText("AC");
                break;
            case 105:
            case 106:
                holder.btnNumber.setText("");
                break;
            case 10:
                holder.btnNumber.setText(".");
                holder.btnNumber.setBackgroundColor(mContext.getResources().getColor(R.color.graye3));
                break;
        }
        //监听事件----------------------------
        if (position == 11) {
            holder.btnNumber.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (position == 11) {
//                            switch (event.getAction()) {
//                                case MotionEvent.ACTION_DOWN:
//                                    holder.btnNumber.setBackgroundResource(R.mipmap.ic_pay_del1);
//                                    break;
//                                case MotionEvent.ACTION_MOVE:
//                                    holder.btnNumber.setBackgroundResource(R.mipmap.ic_pay_del1);
//                                    break;
//                                case MotionEvent.ACTION_UP:
//                                    holder.btnNumber.setBackgroundResource(R.mipmap.ic_pay_del0);
//                                    break;
//                            }
                    }
                    return false;
                }
            });
        }
        holder.btnNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mKeyNum != null) {
                    mKeyNum.onClick(listNumber.get(position));
                }
                System.out.println("点击：" + listNumber.get(position));
//                if (position >= 6 && position <= 14) {//0-9按钮
////                        if (strPass.length() == 6) {
////                            return;
////                        } else {
////                            strPass = strPass + listNumber.get(position);//得到当前数字并累加
////                            mTvPass[strPass.length() - 1].setText("*"); //设置界面*
////                            //输入完成
////                            if (strPass.length() == 6) {
////                                mPayClickListener.onPassFinish(strPass);//请求服务器验证密码
////                            }
////                        }
//                    System.out.println("1-9数组点击");
//                }
//                if (position == 9) {//空按钮
//
//                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        public TextView btnNumber;
    }

    public interface KeyOnClick {
        void onClick(Integer keynum);
    }
}
